//
//  SampuxDeleteEquitmentView.m
//  Sampux
//
//  Created by rzm on 16/7/13.
//  Copyright © 2016年 rzm. All rights reserved.
//

#import "SampuxDeleteEquitmentView.h"


@interface SampuxDeleteEquitmentView ()
@property (nonatomic, strong) UIView *backView;

@end
@implementation SampuxDeleteEquitmentView

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(deleteEquiment:) name:NOTIFICATION_RESPONSE_RELIEVE_DEVICE object:nil];
        [self loadUI];
    }
    return self;
}

- (void)loadUI
{
    CGFloat font = 8.;
    if (IS_IPHONE_4_OR_LESS) {
        font = 14.;
    }else if (IS_IPHONE_5_OR_LESS)
    {
        font = 14.;
    }else if (IS_IPHONE_6)
    {
        font = 17.;
    }else
    {
        font = 17.;
    }
    UIView *view = [[UIView alloc]initWithFrame:[UIScreen mainScreen].bounds];
    [self addSubview:view];
    view.backgroundColor = THBlackAlplaColor(0.6);
    view.alpha = 1;
    
    _backView = [[UIView alloc]initWithFrame:CGRectMake(kScreenWidth*0.08, kScreenHeight * 0.3 , kScreenWidth*0.84, kScreenHeight  * 0.4)];
    [self addSubview:_backView];
    _backView.backgroundColor = THWhiteAlplaColor(0.8);
    [_backView setCorner:5];
    
    UILabel *lab = [UILabel createLabelWithText:@"删除设备" frame:CGRectMake(0, 0, _backView.width, _backView.height * 0.2) font:THFont(font)];
    lab.textAlignment = NSTextAlignmentCenter;
    lab.textColor = THSystemWhiteColor;
    [_backView addSubview:lab];
    lab.backgroundColor = THDeFaultBlueColor;
    
    
    NSString *warnStr = @"设备删除后您将不再拥有此设备的使用权。";
    CGSize maxSize = CGSizeMake(_backView.width - _backView.width *0.05, MAXFLOAT);
    CGSize strSize = [SampuxGlobalTool getSizeWithString:warnStr andFont:THFont(font) andMaxSize:maxSize];
    
   _warnLab = [UILabel createLabelWithText:warnStr frame:CGRectMake(_backView.width *0.08, lab.bottom + Sampux6HScale(20), _backView.width * 0.84, strSize.height) font:THFont(font)];
    _warnLab.numberOfLines = 0;
    _warnLab.textColor = THBlackAlplaColor(0.6);
    [_backView addSubview:_warnLab];
    
    
    UILabel *yesLab = [UILabel createLabelWithText:warnStr frame:CGRectMake(_backView.width *0.08, _warnLab.bottom + Sampux6HScale(15), _backView.width * 0.84, strSize.height) font:THFont(font)];
    yesLab.font = THFont(font);
    yesLab.numberOfLines = 0;
    yesLab.text = @"确认要删除此设备吗？";
    yesLab.textColor = THBlackAlplaColor(0.6);
    [_backView addSubview:yesLab];

    
    UIButton *yesBtn = [UIButton createButton];
    yesBtn.frame = CGRectMake(_backView.width*0.2,yesLab.bottom + Sampux6HScale(10),  _backView.width /5, _backView.width /5);
    [_backView addSubview:yesBtn];
    [yesBtn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    yesBtn.tag = 0;
    [yesBtn setImage:imageNamed(@"yes") forState:UIControlStateNormal];
    
    
    UIButton *noBtn = [UIButton createButton];
    noBtn.frame = CGRectMake(_backView.width*0.6,yesLab.bottom + Sampux6HScale(10),  _backView.width*0.2, _backView.width*0.2);
    [_backView addSubview:noBtn];
    [noBtn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    noBtn.tag = 1;
    [noBtn setImage:imageNamed(@"no") forState:UIControlStateNormal];
    _backView.height = noBtn.bottom + Sampux6HScale(20);

}

- (void)btnClick:(UIButton *)btn
{
    if (btn.tag == 0) {
        [GBInterMaster_CommForSystem requestRelieveDevice:0 withDeviceAccount:self.model.accountStr];
    } else {
        if (self.cancelBlock) {
            self.cancelBlock();
        }
    }
    [self cancelDeleteEquipMent];

}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [[event allTouches] anyObject];
    CGFloat ly = [touch locationInView: self.backView].y;
    if ( ly < 0 || ly > self.backView.height ) { //点标题栏也会消失，点其他地方捕捉不到了
        [self cancelDeleteEquipMent];
    }
    [super touchesBegan:touches withEvent:event];
}

- (void)cancelDeleteEquipMent
{
    dispatch_async(dispatch_get_main_queue(), ^{
        [self removeFromSuperview];
    });
}

- (void)deleteEquiment:(NSNotification *)notification
{
    id userInfo = [notification userInfo];
    if(userInfo == nil){
        return;
    }
    NSDictionary * result = userInfo;
    int msgNumber = [[result objectForKey:@"msgNumber"] intValue];
    int responseCode = [[result objectForKey:@"responseCode"] intValue];
    NSLog(@"deleteEquiment == 结果，消息序号：%d，响应码：%x",msgNumber,responseCode);
    if(responseCode == 0x00)
    {
        [[NSNotificationCenter defaultCenter]postNotificationName:@"setParameterSuccess" object:nil];
        if (self.deleteBlock) {
            self.deleteBlock();
        }
    }else
    {
        [SVProgressHUD showErrorWithStatus:@"删除设备失败"];
//        [MBProgressHUD showError:@"删除设备失败"];
    }
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}


@end
